package com.sunfeng.chap3;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * @Author : 孙峰
 * @Description:  获得ip地址
 * @Date : 2021/10/11  10:56
 */
public class Test2 {
    public static void main(String[] args) throws SocketException {
        Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
        while (networkInterfaces.hasMoreElements()){
            NetworkInterface networkInterface = networkInterfaces.nextElement();
            Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();
            while (inetAddresses.hasMoreElements()){
                InetAddress inetAddress = inetAddresses.nextElement();

                System.out.println(inetAddress.getCanonicalHostName());
                System.out.println(inetAddress.getHostName());
                System.out.println(inetAddress.getHostAddress());

            }
            System.out.println();
        }
    }
}
